//
//  BaseTabBarController.m
//  EquesCat
//
//  Created by Chun Hung Cheung on 2024/12/17.
//

#import "BaseTabBarController.h"
#import "DevicesViewController.h"
#import "MyViewController.h"
#import "EquesBusiness/YKIPinfo.h"
#import "EquesBusiness/YKBusinessFramework.h"
#import "FBDevicesList.h"
#import "VL5CallVideoVController.h"
#import "AppDelegate.h"
#import "CallReminderViewController.h"
@interface BaseTabBarController ()
@property(nonatomic,strong)NSMutableArray *childArray;
@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addAllChildViewControllers];
     self.viewControllers = self.childArray;
     self.selectedIndex = 0;
    
    
    /*注册websocket通知 用于接受消息回调 必须注册*/
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RecvSocketMessage:) name:equesOnMessageResultNotification object:nil];
    
}

- (void)RecvSocketMessage:(NSNotification*)notification
{
    NSDictionary *loginresult = [notification object];
     
    NSString *strMethod = [loginresult objectForKey:@"method"];
    

    
    // 登录成功
   if ([strMethod isEqualToString:LOGIN_V2]){

        
        int code = [[loginresult objectForKey:@"code"] intValue];
        
        if (code == 4000) {
            YKIPinfo *info = [[YKIPinfo bg_find:@"YKIPinfo" where:nil] firstObject];
            info.auto_login = YES;
           [info bg_saveOrUpdate];
           
            NSString *usr_bid = [loginresult objectForKey:@"bid"];
            
            //保存用户bid 用于一键开锁
            [[NSUserDefaults standardUserDefaults] setValue:usr_bid forKey:@"usr_bid"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            
           //获取用户名名下的设备
           [YKBusinessFramework equesGetDeviceList];
            
        }else{
            
             NSString *reason = [loginresult objectForKey:@"reason"];
            [SVProgressHUD showErrorWithStatus:reason duration:5];
            
            
            AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
           [delegate login_out];
            
            
        }
         /*
             bid = 69355e53029c797300e218d08f1a7cc4;
             code = 4000;
             "head_portrait" = "http://eqhome.oss-cn-hangzhou.aliyuncs.com/user_avatar/69355e53029c797300e218d08f1a7cc4.jpg";
             "is_fcm" = 0;
             method = "login_v2";
             "nick_name" = "\U6d77\U54e5";
             phone = 1;
             "push_service" = jiguang;
             reason = success;
             "reg_time" = 1567670434000;
             relayip = "121.40.104.140";
             relayport = 8000;
             srvflx = "39.144.45.116";
             star =     (
             );
             stunip = "120.55.127.3";
             stunport = 1512;
             tinyid = 18016392532;
             uid = 811be5020a1a037140210c70ae0921ff;
             username = 18016392532; // 登录成功的结果 返回的用户名
          
          4000 登录成功
          4103 4102 用户名不存在
          4114 4101 密码错误
          4609 token不存在
          
         */

    
    
    //用户的token回调
   }else if ([strMethod isEqualToString:GETOK]){
       
       
       
   }else if ([strMethod isEqualToString:SETNICK]){
       [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadNickname" object:nil];
   }
   
    //用户返回的设备列表
   else if ([strMethod isEqualToString:BDYLIST]){
        
        
        /*
         {
             // 当前账号下的数组列表
             bdylist =     (
                         {
                     bid = 18aa0edd092a8976200d0fb0a367e4c7;
                     brandId = 0;
                     capability =             {
                         cloud =                 {
                             status = expired;
                         };
                         cloudInfo =                 {
                             favoriteLimit = 10000;
                             favoriteSize = 100G;
                             length = 6;
                             lengthUnit = month;
                             rolloverDay = 45;
                         };
                         control = 1;
                         "default_rollover_day" = 3;
                         face =                 {
                             status = expired;
                         };
                         faceapi = baidu;
                         many = 1;
                         shadow = 1;
                         share = 1;
                         "show_pir_warn" = 0;
                         sip = 1;
                         voiceScenario =                 (
                             "low_power",
                             "wrong_pw",
                             ring
                         );
                     };
                     defaultNick = "\U53ef\U89c6\U5bf9\U8bb2\U667a\U80fd\U9501";
                     "device_class" = 11;
                     group = "";
                     intercom = 1;
                     "is_share" = 0;
                     name = "E0D31A850189-18aa0edd";
                     role = 1024;
                     speech = 1;
                 }
             );
             bindingTime = 1734403245000;
             local =     {
                 favorite =         {
                     status = expired;
                 };
                 "has_favorite" = 1;
             };
             localupg = 1;
             method = bdylist;
         
         
             //在线的设备列表数组
             onlines =     (
         
             );
         }
         
         */
        //存储设备列表
        NSArray *bdylist_array = [loginresult objectForKey:@"bdylist"];
        
        NSMutableArray *array = [NSMutableArray array];
        
        for (NSDictionary *list_dict in bdylist_array) {
            FBDevicesList *list = [[FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"bid"),bg_sqlValue([list_dict objectForKey:@"bid"])]] firstObject];
        
            if (!list) {
                 FBDevicesList *list = [FBDevicesList FBDevicesListWithdict:list_dict];
                [list bg_save];
            }else{
                 list.bid = [list_dict objectForKey:@"bid"];
                 list.nick = [list_dict objectForKey:@"nick"];
                [list bg_saveOrUpdate];
            }
            
            if (![array containsObject:[list_dict objectForKey:@"bid"]]) {
                 [array addObject:[list_dict objectForKey:@"bid"]];
            }
            
        }
        
        NSArray *list_array = [FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken])]];
        
        NSMutableArray *array2 = [NSMutableArray array];
        
        for (FBDevicesList *list in list_array) {
            if (![array2 containsObject:list.bid]) {
                 [array2 addObject:list.bid];
            }
        }
        
       NSArray *Array2 = [self deal_array:array2 array2:array];
        if (Array2.count > 0) {
            for (NSString *bid in Array2) {
                BOOL delete = [FBDevicesList bg_delete:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"bid"),bg_sqlValue(bid)]];
                if (delete) {
                    NSLog(@"删除重复的数据成功 -- %@",bid);
                }
            }
        }
        

        //更新状态
        NSArray *onlines_array = [loginresult objectForKey:@"onlines"];
        for (NSDictionary *list_dict in onlines_array) {
            FBDevicesList *list = [[FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"bid"),bg_sqlValue([list_dict objectForKey:@"bid"])]] firstObject];
            if (list) {
                list.remoteupg = [[list_dict objectForKey:@"remoteupg"] intValue];
                list.status = [[list_dict objectForKey:@"stat"] intValue];
                [list bg_saveOrUpdate];
            }
        }
        
        
        if (onlines_array.count == 0) {
            
            NSArray *list_array = [FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken])]];
            
            for (FBDevicesList *list in list_array) {
                 list.status = 0;
                [list bg_saveOrUpdate];
            }
            
        }
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadList" object:nil];
        
        
        [self getdeviceInfo];
        
        
     //设备初始化 同步设备信息
    }else if ([strMethod isEqualToString:MSG]){
        
        
        NSString *action = [loginresult objectForKey:@"action"];
        if ([action isEqualToString:UPDATE_SHADOW_DEVICE] || [action isEqualToString:UPDATE_SHADOW_SETTINGS] || [action isEqualToString:INIT_SHADOW_DEVICE] ){
            
            
        }
        
        /*
         {
             action = "init_shadow_device";
             method = msg;
             payload =     {
                 deviceId = 494d396a0929d68420800c3050f3533c;
                 lastUpdate = 1734435182136;
                 settings =          { // 设备设置信息
                     "alarm_enable" = 0;
                     "alive_start_time" = "00:00";
                     "alive_stop_time" = "23:59";
                     "alm_mod" = 1;
                     "disturb_end_time" = "08:00";
                     "disturb_start_time" = "22:00";
                     "disturb_status" = 0;
                     "lcd_timeout" = 10;
                     "show_open_lock_button" = 1;
                     "time_zone" = "+8";
                     "video_time" = 5;
                     "voice_count" = 1;
                     "voice_index" = 0;
                     "voice_status" = 0;
                 };
                 state =         { // 设备状态信息
                     "brand_id" = 102148;
                     bssid = 0e4b541eb842;
                     "camera_id" = 1;
                     "capture_rotate" = 5;
                     "door_stat" = 0;
                     "dual_camera" = 0;
                     "function_support" = 00001001;
                     ip = "192.168.1.101";
                     "is_uac" = 0;
                     lkmang = 2;
                     "locked_stat" = 0;
                     "locked_time" = 0;
                     "no_alive" = 0;
                     "power_mode" = 1;
                     "sub_camera_id" = 5;
                     temporary = 0;
                     "usb_vnum" = 10009;
                     "video_f" = H264;
                     "videocall_height" = 480;
                     "videocall_width" = 640;
                     vnum = 10066;
                     voltage = "8.388000";
                     "wifi_level" = 4;
                     "wifi_rssi" = "-42";
                     "wifi_ssid" = ps;
                 };
             };
         }
         
         */
    
        // 删除设备成功
    }else if ([strMethod isEqualToString:RMBDY_RESULT]){
        
        NSString *bid =  [loginresult objectForKey:@"bid"];
        int code =  [[loginresult objectForKey:@"code"] intValue];
        
        if (code == 4000 && bid.length > 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"rmbdy_result" object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteDevices" object:nil];
            BOOL delete = [FBDevicesList bg_delete:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"bid"),bg_sqlValue(bid)]];
            if (delete) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadList" object:nil];
            }
        }
        
     //绑定设备成功的结果 on_addbdy_result
    }else if([strMethod isEqualToString:ON_ADDBDY_RESULT]){
        /*
         {
             "added_bdy" =     {
                 bid = 494d396a0929d68420800c3050f3533c;
                 capability =         {
                     cloud =             {
                         status = expired;
                     };
                     "default_rollover_day" = 3;
                     faceapi = baidu;
                     shadow = 1;
                     share = 1;
                     "show_pir_warn" = 0;
                     voiceScenario =             (
                         "low_power"
                     );
                 };
                 defaultNick = "\U667a\U80fd\U5582\U98df\U673a";
                 "device_class" = 12;
                 "is_share" = 0;
                 name = "E0D31AB0001A-494d396a";
                 role = 24002;
             };
             code = 4000;
             local =     {
                 favorite =         {
                     status = expired;
                 };
                 "has_favorite" = 1;
             };
             method = "on_addbdy_result";
             onlines =     (
                         {
                     bid = 494d396a0929d68420800c3050f3533c;
                     remoteupg = 0;
                     stat = 1;
                     status = 1;
                     uid = 494d396a0929d68420800c3050f3533c;
                 }
             );
         }
         */
        
     //设备上下线
    }else if ([strMethod isEqualToString:DEVST]){
        
        /*
         {
             bid = 18aa0edd092a8976200d0fb0a367e4c7;
             method = devst;
             name = "E0D31A850189-18aa0edd";
             remoteupg = 0;
             role = 1024;
             stat = 1;
             status = 1;
             type = 1024;
             uid = 18aa0edd092a8976200d0fb0a367e4c7;
         }
         */
        
        FBDevicesList *list = [[FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"bid"),bg_sqlValue([loginresult objectForKey:@"bid"])]] firstObject];
        
         list.status = [[loginresult objectForKey:@"stat"] intValue];
        [list bg_saveOrUpdate];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:DEVST object:loginresult];
        
    //喂食告警 喂食记录 或 锁报警 锁记录
    }else if ([strMethod isEqualToString:SMART_LOCK_ALARM] || [strMethod isEqualToString:SMART_LOCK_MSG]){
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"alarmReloadData" object:nil];
        
        
    // 喂食设备操作消息透传
    }else if([strMethod isEqualToString:CONTROL]){
        
        if ([loginresult objectForKey:@"reset"]) {
            return;
        }
        
        // 喂食状态
        if([loginresult objectForKey:@"feed_state"]){
            
            NSString *status =  [loginresult objectForKey:@"feed_state"];
            if ([status isEqualToString:@"doneing"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"manual_feed" object:loginresult];
            }
            // 喂食计划
        }else if([loginresult objectForKey:@"meal_plan"]){
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"meal_plan" object:loginresult];
            
        }
        
        if ([[loginresult objectForKey:@"code"] intValue] == 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"get_plan" object:nil];
        }
        
    }else if([strMethod isEqualToString:GET_CONTROL]){
        
        if([[loginresult objectForKey:@"code"] intValue] == 0){
            [[NSNotificationCenter defaultCenter] postNotificationName:@"get_control" object:loginresult];
        }
    }else if ([strMethod isEqualToString:RESTORE_DEVICES_R700_RESULT]){
        
        NSLog(@"恢复出厂设置结果--->restore_devices_r700_result");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"restore_devices_r700_result" object:loginresult];
      
        // 设备被其它用户删除给出的通知
    }else if ([strMethod isEqualToString:ON_BDY_REMOVED]){
        
   
        [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteDevices" object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
        
        // 宠物mcu升级
    }else if ([strMethod isEqualToString:UPGRADE_MCU_RES]){
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"upgrade_mcu_res" object:loginresult];
        
        // 宠物对讲版本升级
    }else if ([strMethod isEqualToString:UPGRADE_RESULT])
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"devicesCheckUpdate" object:loginresult];
        
    }else if ([strMethod isEqualToString:CALL_V2])
    {
        
        [self call_v2:loginresult];
        
    }else if([strMethod isEqualToString:FILETRANS]){
        /*
         {
             context = "cid=100";
             fid = XBVrvarxufwjZaX4yocQ3L990wLA1pP6;
             filetime = 1743501307099;
             from = 18aa0edd092a8976200d0fb0a367e4c7;
             "has_preview" = 1;
             method = filetrans;
             "op_type" = 0;
         }
         */
        
        [[NSNotificationCenter defaultCenter] postNotificationName:FILETRANS object:loginresult];
    }
    
    
    

    
}


- (void)call_v2:(NSDictionary*)message
{
    /*call_v2 4种状态中state   invite: 邀请 设备按门铃时 app在前台收到消息需要处理；
                             answer: 回复 主动呼叫时设备回复 可以处理
                             hangup: 挂断 设备主动挂断时 视频界面挂断
                             calling:设备正在通话中 不支持再呼叫*/
    NSString *strstate = [message objectForKey:@"state"];
    
    /*
         {
           "method": "call_v2",
           "from": "7c71bb3e0a0e60c5201b1fa03f343f12",
           "to": "1005449c0a6d64c5401611407f6da81b",
           "state": "invite",
           "channel_id": 0,
           "ringtime": "1742788004000",
           "peerid": "D6nr1rq3zP62aR4DTVtwdCowIpMctFzR",
           "rtcserverip": "47.97.214.167",
     
     
     
     
           "rtcserverport": 30000,
           "op_type": 0,
           "rtcudpip": "118.31.67.241",
           "rtcudpport": 8800,
           "netType": 0
         }
     */

    //弹出门铃来电界面
    if ([strstate isEqualToString:@"invite"]) {
    

        CallReminderViewController *vc = [[CallReminderViewController alloc]init];
        vc.bid = [message objectForKey:@"from"];
        vc.modalPresentationStyle =  UIModalPresentationFullScreen;
        vc.msgJson = message;
        vc.vl0time = 60;
        [self presentViewController:vc animated:YES completion:^{
            
        }];
    
    }else if ([strstate isEqualToString:@"answer"]){
        
        
    }else{
        
        
    }
    
}




- (NSArray*)deal_array:(NSArray*)array1 array2:(NSArray*)array2
{

    // 将数组转换为集合去重
    NSSet *set1 = [NSSet setWithArray:array1];
    NSSet *set2 = [NSSet setWithArray:array2];
    
    // 获取两个集合的差集（即不重复的元素）
    NSMutableSet *uniqueSet1 = [set1 mutableCopy];
    [uniqueSet1 minusSet:set2]; // 从 set1 中去掉 set2 中的元素，保留不重复的部分
    
    // 转换回数组
    NSArray *uniqueArray1 = [uniqueSet1 allObjects];
    
    return uniqueArray1;
}


#pragma mark - 1.添加所有的子控制器
- (void)addAllChildViewControllers
{
    // 1.1 设备
    [self addOneViewController:[[DevicesViewController alloc] init] image:@"device_bottom_unselect" selectedImage:@"device_bottom_select" title:NSLocalizedString(@"设备", nil)];

    // 1.4 我的
    [self addOneViewController:[[MyViewController alloc] init] image:@"mine_bottom_unselect" selectedImage:@"mine_bottom_select" title:NSLocalizedString(@"我的", nil)];
}



#pragma mark - 1.1.添加一个子控制器的方法
- (void)addOneViewController:(UIViewController *)childViewController image:(NSString *)imageName selectedImage:(NSString *)selectedImageName title:(NSString *)title
{
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childViewController];
        // 1.1.1 设置tabBar文字
        nav.tabBarItem.title = title;
        // 1.1.2 设置正常状态下的图标
        if (imageName.length) { // 图片名有具体
            nav.tabBarItem.image = [UIImage imageNamed:imageName];
            // 1.1.3 设置选中状态下的图标
            UIImage *image = [UIImage imageNamed:selectedImageName];
            nav.tabBarItem.selectedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        
        nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -6);
        nav.tabBarItem.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0);
    
    
        // 1.1.5 添加tabBar为控制器的子控制器
        [self.childArray addObject:nav];
}



-(NSMutableArray*)childArray
{
    if (!_childArray) {
        _childArray = [NSMutableArray array];
    }
    return _childArray;
    
}


#pragma mark - 2.设置tabBarItems的文字属性
+ (void)load
{
    //  设置TabBar的背景
    UITabBar *tabBarAppearance = [UITabBar appearance];
     tabBarAppearance.backgroundColor = [UIColor clearColor];
     tabBarAppearance.translucent = NO;
    

    if (@available(iOS 13.0, *)) {
            [[UITabBar appearance] setUnselectedItemTintColor:[UIColor colorWithRed:169 / 255.0 green:172 / 255.0 blue:174 / 255.0 alpha:1.0]];
     }
    
    //  正常状态下的文字
    NSMutableDictionary *normalAttr = [NSMutableDictionary dictionary];
    normalAttr[NSForegroundColorAttributeName] = [UIColor colorWithRed:169 / 255.0 green:172 / 255.0 blue:174 / 255.0 alpha:1.0];
    normalAttr[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    //  选中状态下的文字
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSForegroundColorAttributeName] = [HexChangeRGB colorWithHexString:@"#FB8D31"];
    selectedAttr[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    //  统一设置UITabBarItem的文字属性
    UITabBarItem *item = [UITabBarItem appearance];
    //  设置UITabBarItem的正常状态下的文字属性
    [item setTitleTextAttributes:normalAttr forState:UIControlStateNormal];
    //  设置UITabBarItem的选中状态下的文字属性
    [item setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];
    // 记录当前选中的下标
    self.selectedIndex = index;
    return YES;
}

- (void)getdeviceInfo
{
    
    NSArray *list_array = [FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken])]];
    
    
    for (FBDevicesList *device in list_array) {
        
        [AFNHTTPSRequest httpsRequestWithUrl:[YKBusinessFramework get_shadow_state:device.bid] errorHint:^(id StaterrorData) {
            NSLog(@"获取状态失败-->%@",StaterrorData);
            
        } result:^(id StateData) {
            
            if ([[StateData objectForKey:@"code"] intValue] == 0){
                NSDictionary *stateDict = [StateData objectForKey:@"state"];
                
                FBDevicesList *stateInfo = [[FBDevicesList bg_find:@"FBDevicesList" where:[NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"userName"),bg_sqlValue([Tooles getuserToken]),bg_sqlKey(@"bid"),bg_sqlValue(device.bid)]] firstObject];
                
                if (stateInfo) {
                    
                    stateInfo.wifi_ssid = [stateDict objectForKey:@"wifi_ssid"];
                    stateInfo.wifi_rssi = [stateDict objectForKey:@"wifi_level"];
                    stateInfo.camera_id = [[stateDict objectForKey:@"camera_id"] intValue];
                    if ([stateDict objectForKey:@"capture_rotate"]) {
                        stateInfo.capture_rotate = [[stateDict objectForKey:@"capture_rotate"] intValue];
                    }
                    if ([stateDict objectForKey:@"sub_camera_id"]) {
                        stateInfo.sub_camera_id = [[stateDict objectForKey:@"sub_camera_id"] intValue];
                    }
                    if ([stateDict objectForKey:@"toWayVideo"]) {
                        stateInfo.toWayVideo = [[stateDict objectForKey:@"toWayVideo"] intValue];
                    }
                    if ([stateDict objectForKey:@"is_support_voice"]) {
                        stateInfo.is_support_voice = [[stateDict objectForKey:@"is_support_voice"] intValue];
                    }
                    
                    
                    if ([stateDict objectForKey:@"inside_call_ppi"]) {
                        stateInfo.inside_call_ppi = [stateDict objectForKey:@"inside_call_ppi"];
                    }
                    if ([stateDict objectForKey:@"lock_type"]) {
                        stateInfo.lock_type = [[stateDict objectForKey:@"lock_type"] intValue];
                    }
                    if ([stateDict objectForKey:@"call_spacing"]) {
                        stateInfo.call_spacing = [[stateDict objectForKey:@"call_spacing"] intValue];
                    }else{
                        stateInfo.call_spacing = -1;
                    }
                    if ([stateDict objectForKey:@"aov_type"]) {
                        stateInfo.aov_type = [[stateDict objectForKey:@"aov_type"] intValue];
                    }
                    if ([stateDict objectForKey:@"temporary"]) {
                        stateInfo.temporary = [[stateDict objectForKey:@"temporary"] intValue];
                    }
                    
                    if ([stateDict objectForKey:@"bat_lvl"]) {
                        stateInfo.bat_lvl = [[stateDict objectForKey:@"bat_lvl"] intValue];
                    }else{
                        stateInfo.bat_lvl = -1;
                    }
                    
                    stateInfo.app_call = [[stateDict objectForKey:@"app_call"] intValue];
                    stateInfo.lkmang = [[stateDict objectForKey:@"lkmang"] intValue];
                    stateInfo.lck_ota = [[stateDict objectForKey:@"lck_ota"] intValue];
                    stateInfo.netType = [[stateDict objectForKey:@"netType"] intValue];
                    stateInfo.call_rtc = 1;
                    stateInfo.no_alive = [[stateDict objectForKey:@"no_alive"] intValue];
                    stateInfo.is_uac = [[stateDict objectForKey:@"is_uac"] intValue];
                    stateInfo.brand_id = [[stateDict objectForKey:@"brand_id"] intValue];
                    if ([stateDict objectForKey:@"wifi_rssi"]) {
                        stateInfo.wifi_rssi = [stateDict objectForKey:@"wifi_rssi"];
                    }
                    stateInfo.vnum = [stateDict objectForKey:@"vnum"];
                    stateInfo.flexible_unlock = [[stateDict objectForKey:@"flexible_unlock"] intValue];
                    stateInfo.dual_camera = [[stateDict objectForKey:@"dual_camera"] intValue];
                    stateInfo.bssid = [stateDict objectForKey:@"bssid"];
                    stateInfo.keep_alive = [[stateDict objectForKey:@"keep_alive"] intValue];
                    stateInfo.usb_upgrad = [[stateDict objectForKey:@"usb_upgrad"] intValue];
                    stateInfo.video_f = [stateDict objectForKey:@"video_f"];
                    stateInfo.usb_vnum = [stateDict objectForKey:@"usb_vnum"];
                    stateInfo.ip = [stateDict objectForKey:@"ip"];
                    stateInfo.videocall_width = [[stateDict objectForKey:@"videocall_width"] intValue];
                    stateInfo.videocall_height = [[stateDict objectForKey:@"videocall_height"] intValue];
                    [stateInfo bg_saveOrUpdateAsync:^(BOOL isSuccess) {
                        
                    }];
                    
                }
            }
            
        }];
        
    }
    
}
@end
